package gui.windows;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import gui.GUIHandler;
import gui.GUIManager;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JTextField;

import app.AppGlobals;
import chat.ChatHandler;
import javax.swing.SpringLayout;
import javax.swing.JList;
import room.Room;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JButton;

import network.ChatMessage;
import network.enums.ServerPackets;

@SuppressWarnings("serial")
public class ChatTab extends JPanel {
	private JTextField chatInput;

	public ChatTab()
	{
		setLayout(null);
		this.setBounds(32, 102, 976, 471);
		
		JPanel panel_16 = new JPanel();
		panel_16.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_16.setBounds(10, 11, 748, 400);
		this.add(panel_16);
		panel_16.setLayout(null);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		scrollPane_4.setBounds(0, 0, 748, 400);
		panel_16.add(scrollPane_4);
		
		JTabbedPane chatWindows= new JTabbedPane(JTabbedPane.BOTTOM);
		GUIManager.chatTabChatWindows = chatWindows;
		scrollPane_4.setViewportView(chatWindows);
		ChatHandler.createChatTab("System",false,true);
		ChatHandler.getChatWindow("System").writeSystemMessage("Welcome to DevPro's chat system.",ChatHandler.autoScroll);
		ChatHandler.getChatWindow("System").writeSystemMessage("Press the channel list button to join a chat channel.",ChatHandler.autoScroll);
		
		JTabbedPane tabbedPane_2 = new JTabbedPane(JTabbedPane.BOTTOM);
		tabbedPane_2.setBorder(null);
		tabbedPane_2.setBounds(768, 11, 193, 426);
		this.add(tabbedPane_2);
		
		JPanel panel_17 = new JPanel();
		panel_17.setBorder(null);
		tabbedPane_2.addTab("Users", null, panel_17, null);
		panel_17.setLayout(null);
		
		JTabbedPane tabbedPane_3 = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane_3.setBorder(null);
		tabbedPane_3.setBounds(0, 0, 188, 393);
		panel_17.add(tabbedPane_3);
		
		JPanel panel_20 = new JPanel();
		panel_20.setBorder(null);
		tabbedPane_3.addTab("Online", null, panel_20, null);
		panel_20.setLayout(null);
		
		DefaultListModel<String> userListModel = new DefaultListModel<String>();
		AppGlobals.userListModel = userListModel;
		JList<String> userList = new JList<String>(userListModel);
		userList.setBorder(new LineBorder(new Color(0, 0, 0)));
		userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		userList.setDoubleBuffered(true);
		userList.setBounds(5, 5, 173, 355);
		panel_20.add(userList);
		GUIManager.chatTabUserListBox = userList;
		
		JPanel panel_21 = new JPanel();
		panel_21.setBorder(null);
		tabbedPane_3.addTab("Team", null, panel_21, null);
		panel_21.setLayout(null);
		
		DefaultListModel<String> teamListModel = new DefaultListModel<String>();
		AppGlobals.teamListModel = teamListModel;
		JList<String> teamList = new JList<String>(teamListModel);
		teamList.setBorder(new LineBorder(new Color(0, 0, 0)));
		teamList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		teamList.setDoubleBuffered(true);
		teamList.setBounds(5, 5, 173, 262);
		panel_21.add(teamList);
		
		JPanel panel_22 = new JPanel();
		panel_22.setBorder(null);
		tabbedPane_3.addTab("Friends", null, panel_22, null);
		panel_22.setLayout(null);
		
		DefaultListModel<String> friendListModel = new DefaultListModel<String>();
		AppGlobals.friendListModel = friendListModel;
		JList<String> friendList = new JList<String>(friendListModel);
		friendList.setBorder(new LineBorder(new Color(0, 0, 0)));
		friendList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		friendList.setDoubleBuffered(true);
		friendList.setBounds(5, 5, 173, 355);
		panel_22.add(friendList);
		
		JPanel panel_18 = new JPanel();
		tabbedPane_2.addTab("Ignore", null, panel_18, null);
		panel_18.setLayout(null);
		
		DefaultListModel<String> ignoreListModel = new DefaultListModel<String>();
		AppGlobals.ignoreListModel = ignoreListModel;
		JList<String> ignoreList = new JList<String>(ignoreListModel);
		ignoreList.setBorder(new LineBorder(new Color(0, 0, 0)));
		ignoreList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ignoreList.setDoubleBuffered(true);
		ignoreList.setBounds(5, 5, 173, 377);
		panel_18.add(ignoreList);
		
		chatInput = new JTextField();
		chatInput.setBounds(10, 415, 641, 20);
		add(chatInput);
		chatInput.setColumns(10);
		
		final JButton ChannelBtn = new JButton("Channel List");
		ChannelBtn.setBounds(661, 415, 97, 23);
		add(ChannelBtn);
		ChannelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AppGlobals.gameServer.sendPacket(ServerPackets.ChannelList);
				ChannelBtn.setEnabled(false);
			}
		});
		GUIManager.chatTabChannelBtn = ChannelBtn;
		
		chatInput.addKeyListener(new KeyAdapter(){
			public void keyReleased(KeyEvent e ){
				if(e.getKeyCode() == KeyEvent.VK_ENTER ){
					if(chatInput.getText() == "")
						return;
						ChatHandler.sendMessage(ChatHandler.getSelectedChatWindow().Name, chatInput.getText(), false);
						chatInput.setText("");
				}}});
	}
}
